package com.wisedu.jwjob.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Base64;
/**
 * 
 * Description: desc加密
 *
 * @author liurk
 * @date 2019年8月19日
 * @version 1.0
 */
public class DESCUtils {
    /**
      * 算法名称
     */
    private final String KEY_ALGORITHM = "DES";
    /**
       * 算法名称/加密模式/填充方式
     */
    private final String CIPHER_ALGORITHM_CBC = "DES/CBC/NoPadding";
    /**
     * key 密钥
     */
    private String key = "xiada@param!encryp";
    /**
     * IvParameterSpec参数
     */
    private String iv = "87651234";

    public static void main(String[] args) {
		String teString = "checktime=2019-08-21&studentid=30220182201089";
		DESCUtils descbcUtils = new DESCUtils();
		String encrypt = descbcUtils.encrypt(teString);
		System.out.println(encrypt);
		String decrypt = descbcUtils.decrypt(encrypt);
		System.out.println(decrypt);
	}
    
    /**
     * 生成初始化向量
     *
     * @return
     */
    public byte[] getIV() throws Exception {
        byte[] bytes = iv.getBytes();
        return bytes;
    }

    /**
     * 生成密钥
     *
     * @return
     * @throws Exception
     */
    public byte[] generateKey() throws Exception {
        byte[] bytes = key.getBytes();
        return bytes;
    }


    /**
     * DES 加密
     *
     * @param data 原文
     * @return 密文
     * @throws Exception
     */
    public String encrypt(String data){
        byte[] enc = null; //加密
		try {
			DESKeySpec dks = new DESKeySpec(generateKey());
			SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
			SecretKey secretKey = factory.generateSecret(dks);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));
			enc = cipher.doFinal(addByte(data.getBytes()));
		} catch (Exception e) {
			// TODO: handle exception
		}
        return Base64.encodeBase64URLSafeString(enc);
    }

    /**
     * DES 解密
     *
     * @param data 密文
     * @return 明文、原文
     * @throws Exception
     */
    public String decrypt(String data){
        byte[] dec = null; // 解密
		try {
			DESKeySpec dks = new DESKeySpec(generateKey());
			SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
			SecretKey secretKey = factory.generateSecret(dks);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
			IvParameterSpec ivParameterSpec = new IvParameterSpec(getIV());
			cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
			dec = cipher.doFinal(Base64.decodeBase64(data));
		} catch (Exception e) {
			// TODO: handle exception
		}
        return new String(deleteByte(dec));
    }

    /*不足8的倍数，进行补位*/
    private byte[] addByte(byte[] data) {
        int num = 0;
        if (data.length % 8 != 0) {
            num += 8 - (data.length % 8) + data.length;
        } else {
            num += 8 + data.length;
        }
        byte[] arr = new byte[num];
        System.arraycopy(data, 0, arr, 0, data.length);
        arr[num - 1] = Byte.valueOf(String.valueOf(num - data.length));
        return arr;
    }

    /*删除补位*/
    private byte[] deleteByte(byte[] data) {
        int n = data[data.length - 1];
        byte[] arr = new byte[data.length - n];
        System.arraycopy(data, 0, arr, 0, data.length - n);
        return arr;
    }

    public String getKey() {
        return key;
    }

    public String getIv() {
        return iv;
    }

}
